package com.solution.admin.customer.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.solution.admin.customer.entity.CustomerLoginHistoryEntity;
import com.solution.admin.customer.service.CustomerLoginHistoryService;
import com.solution.common.controller.BaseController;
import com.solution.common.page.JSONGrid;
import com.solution.common.page.PageBean;
import com.solution.common.page.PageParam;
import com.solution.common.utils.string.StringUtil;

/**
 * 客户登录历史表web控制层
 * @author solin
 * @date 2017-10-31 14:22:42
 */
@RequestMapping("/customerLoginHistory")
@Controller
public class CustomerLoginHistoryController extends BaseController {

	private Logger logger = LoggerFactory.getLogger(CustomerLoginHistoryController.class);

	@Autowired
	private CustomerLoginHistoryService customerLoginHistoryService;
	
	/**
	 * 跳转客户登录历史表查询页面
	 * @param CustomerLoginHistory 接收界面查询条件参数
	 * @param request 请求对象
	 * @return 返回对应的页面视图
	 * @author solin
 	 * @date 2017-10-31 14:22:42
	 */
	@RequestMapping("/toListCustomerLoginHistoryPage")
	public String toListCustomerLoginHistoryPage(CustomerLoginHistoryEntity customerLoginHistory, HttpServletRequest request) {
		return "customer/customerLoginHistory_list";
	}

	/**
	 * 分页查询客户登录历史表列表
	 * @param customerLoginHistory 接收界面查询条件参数
	 * @param request 请求对象
	 * @return 返回对应的页面视图
	 * @author solin
 	 * @date 2017-10-31 14:22:42
	 */
	@RequestMapping("/listCustomerLoginHistoryPage")
	@ResponseBody
	public JSONGrid listCustomerLoginHistoryPage(CustomerLoginHistoryEntity customerLoginHistory, HttpServletRequest request){
		int pageNum=Integer.parseInt(request.getParameter("page").toString());
		int numPerPage=Integer.parseInt(request.getParameter("rows").toString());
		PageParam pageParam = super.getPageParam(request);
		pageParam.setPageNum(pageNum);
		pageParam.setNumPerPage(numPerPage);
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("companyName", request.getParameter("companyName"));
		paramMap.put("userName", request.getParameter("userName"));
		paramMap.put("mobile", request.getParameter("mobile"));
		paramMap.put("customerLevelId", request.getParameter("customerLevelId"));
		try {
			PageBean pageBean = customerLoginHistoryService.listCustomerLoginHistoryPage(pageParam, paramMap);
			return new JSONGrid(pageBean.getRecordList(),pageBean.getTotalCount());
		} catch (Exception e) {
			handleException("分页查询客户登录历史表失败", logger, e);
		}
		return new JSONGrid();
	}
	
	
	/**
	 * 跳转新增页面
	 * @return string 跳转页面路径
	 * @author solin
 	 * @date 2017-10-31 14:22:42
	 */
	@RequestMapping("/toAddCustomerLoginHistory")
	public String toAddCustomerLoginHistory(HttpServletRequest request) {
		request.setAttribute("opFlag", "add");
		return "customer/customerLoginHistory_form";
	}

	/**
	 * 跳转编辑页面
	 * @return string 跳转页面路径
	 * @author solin
 	 * @date 2017-10-31 14:22:42
	 */
	@RequestMapping("/toUpdateCustomerLoginHistory")
	public String toUpdateCustomerLoginHistory(HttpServletRequest request) {
		// 编辑操作
		request.setAttribute("opFlag", "edit");
		long id = Long.valueOf(request.getParameter("id"));
		try {
			CustomerLoginHistoryEntity customerLoginHistory = customerLoginHistoryService.getCustomerLoginHistoryById(id);
			request.setAttribute("customerLoginHistory", customerLoginHistory);
		} catch (Exception e) {
			handleException("跳转编辑客户登录历史表页面错误", logger, e);
		}
		return "customer/customerLoginHistory_form";
	}


	/**
	 * 新增或保存客户登录历史表
	 * @param request 请求对象
	 * @param customerLoginHistoryEntity 客户登录历史表实体
	 * @return
	 * @author solin
 	 * @date 2017-10-31 14:22:42
	 */
	@RequestMapping("/saveOrUpdateCustomerLoginHistory")
	@ResponseBody
	public Map<String, Object> saveCustomerLoginHistory(HttpServletRequest request, CustomerLoginHistoryEntity customerLoginHistoryEntity) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int result = 0;
		try {
			String id = request.getParameter("id");
			if(StringUtil.isNotEmpty(id)){
				result = customerLoginHistoryService.updateCustomerLoginHistory(customerLoginHistoryEntity);
			}else{
				result = (int)customerLoginHistoryService.saveCustomerLoginHistory(customerLoginHistoryEntity);
			}
			resultMap.put("code", result);
		} catch (Exception e) {
			handleException("添加客户登录历史表失败", logger, e);
		}
		return resultMap;
	}
	
}
